<?php

// @formatter:off
/**
 * @file rules.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: rules.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/frontview/actions/content/rules.php $
 */
// @formatter:on

class ActionControllerContentRules extends WCActionController {
	//
	// Public methods.
	public function check() {
		$check = parent::check();

		if($check == WC_HTTPERROR_OK) {
			global $wcProfile;

			if(!$wcProfile->allowedTo(WC_PERM_RULE_LIST)) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_FORBIDDEN;
				$errno = $check;
				$error = "You're not allowed to list rules";
			}
		}

		return $check;
	}
	//
	// Protected methods.
	protected function subRun() {
		$ok = true;

		//
		// Loading global pointers.
		global $wcGroup;
		global $wcProfile;

		$limiter = "rules";
		$limits = WCFVPager::GetLimitsFor($limiter);

		$this->_manager->PATH = "All Rules (page {$limits["page"]})";

		//
		// Selecting only top rules in range.
		$rules = array();
		$items = WCRulesHolder::I()->itemsByGroup($wcGroup);
		$ruleCount = 1;
		uasort($items, "WCRule::NameSort");
		foreach($items as $rule) {
			if($rule->ok()) {
				if($ruleCount >= $limits["min"] && $ruleCount <= $limits["max"]) {
					$rules[] = $rule;
				}
				$ruleCount++;
			}
		}

		WCFVPager::GetAssignsIntoManagerFor($ruleCount, $limiter);

		//
		// Creating multi-array by prefix
		$lists = array();
		$ids = 1;
		foreach($rules as $rule) {
			$name = $rule->name;
			$prefix = strtoupper(isset($name[0]) ? $name[0] : "#");
			if($prefix < 'A' || $prefix > 'Z') {
				$prefix = '#';
			}
			$prefix = "{$prefix}";
			if(!isset($lists[$prefix])) {
				$lists[$prefix] = array(
					"prefix" => $prefix,
					"list" => array()
				);
			}

			$lists[$prefix]["list"][] = WCFVBoxes::GetRulePreviewAssigns($rule);
		}
		ksort($lists);

		$this->_manager->rules_lists = $lists;

		if($wcProfile->allowedTo(WC_PERM_RULE_ADD)) {
			$this->_manager->rules_create = true;
		}

		return $ok;
	}
}
